package com.ych.modules.cms.authorInfc.menu.service.impl;

import com.ych.config.Menu;
import com.ych.modules.cms.authorInfc.menu.service.IMenuService;
import com.ych.modules.base.dbEntity.TSysMenuEntity;
import com.ych.modules.base.service.ITSysMenuService;
import com.ych.utils.UUID;
import com.ych.utils.beanToMap.BeanMapUtil;
import com.ych.utils.menu.MenuTree;
import com.ych.utils.menu.MenuTreeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/21
 * Time: 14:52
 * Description:
 */
@Service("IMenuService")
public class MenuServiceImpl implements IMenuService {
    @Autowired
    private ITSysMenuService itSysMenuService;

    @Override
    public List<TSysMenuEntity> menuByUserId(String userId) {
        //用户菜单列表
        List<TSysMenuEntity> menuIdList = itSysMenuService.getUserMenuList(userId);
        return menuIdList;
    }

    @Override
    public Set<String> perms(String userId) {
        return itSysMenuService.permsByUserId(userId);
    }

    @Override
    public MenuTree list() {
        List<TSysMenuEntity> menuList = itSysMenuService.selectList(null);
        List<Map<String, Object>> menuListMap = new ArrayList<>();
        for (TSysMenuEntity e : menuList) {
            menuListMap.add(BeanMapUtil.convertBean(e));
        }
        MenuTree rootTree = new MenuTree();
        rootTree.setMenuId("0");
        MenuTree menuTree = MenuTreeUtil.packageTree(menuListMap, rootTree);
        return menuTree;
    }

    @Override
    public List<TSysMenuEntity> select() {
        //查询列表数据
        List<TSysMenuEntity> menuList = itSysMenuService.queryNotButtonList();
        //添加顶级菜单
        TSysMenuEntity root = new TSysMenuEntity();
        root.setMenuId("0");
        root.setMenuName("一级菜单");
        root.setParentId("-1");
        menuList.add(root);
        return menuList;
    }

    @Override
    public TSysMenuEntity info(String menuId) {
        return itSysMenuService.selectById(menuId);
    }

    @Override
    public void save(TSysMenuEntity entity) {
        this.verifyForm(entity);
        entity.setMenuId(UUID.randomUUID());
        itSysMenuService.insert(entity);
    }

    @Override
    public void update(TSysMenuEntity entity) {
        this.verifyForm(entity);
        itSysMenuService.updateById(entity);
    }

    @Override
    public void delete(String menuId) {
        //判断是否有子菜单或按钮
        List<TSysMenuEntity> menuList = itSysMenuService.queryListParentId(menuId);
        if (menuList.size() > 0) {
            throw new RuntimeException("请先删除子菜单或按钮");
        }
        itSysMenuService.delete(menuId);
    }


    /**
     * 验证参数是否正确
     */
    private void verifyForm(TSysMenuEntity menu) {
        if (StringUtils.isBlank(menu.getMenuName())) {
            //throw new RRException("菜单名称不能为空");
            throw new RuntimeException("菜单名称不能为空");
        }

        if (menu.getParentId() == null) {
            //	throw new RRException("上级菜单不能为空");
            throw new RuntimeException("上级菜单不能为空");
        }

        //菜单
        if (menu.getMenuType() == Menu.MenuType.MENU.getValue()) {
            if (StringUtils.isBlank(menu.getMenuUrl())) {
                //	throw new RRException("菜单URL不能为空");
                throw new RuntimeException("菜单URL不能为空");
            }
        }

        //上级菜单类型
        String parentType = Menu.MenuType.CATALOG.getValue();
        if (!"0".equals(menu.getParentId())) {
            TSysMenuEntity parentMenu = itSysMenuService.selectById(menu.getParentId());
            parentType = parentMenu.getMenuType();
        }

        //目录、菜单
        if (Menu.MenuType.CATALOG.getValue().equals(menu.getMenuType()) ||
                Menu.MenuType.MENU.getValue().equals(menu.getMenuType())) {
            if (!Menu.MenuType.CATALOG.getValue().equals(parentType)) {
                throw new RuntimeException("上级菜单只能为目录类型");
            }
            return;
        }

        //按钮
        if (Menu.MenuType.BUTTON.getValue().equals(menu.getMenuType())) {
            if (!Menu.MenuType.MENU.getValue().equals(parentType)) {
                throw new RuntimeException("上级菜单只能为菜单类型");
            }
        }
        return;
    }
}
